package cn.stronglink.asset.module.assetmanage.puton.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.plugins.Page;
import cn.stronglink.asset.model.PutOn;
import cn.stronglink.asset.model.PutOnDetail;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.puton.service.PutOnService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.util.WebUtil;

@Controller
@RequestMapping(value = "puton")
public class PutOnController extends AbstractController {

	@Autowired
	private PutOnService putOnService;

	// 查询所有上架单
	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryPutOnList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {
		boolean isAdmin = (boolean) request.getSession().getAttribute("isAdmin");
		if(!isAdmin) {
			params.put("operatorBy", WebUtil.getCurrentUser());
		}
		@SuppressWarnings("unchecked")
		Page<PutOn> page = (Page<PutOn>) super.getPage(params);
		Page<PutOn> data = putOnService.queryPutOnList(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	// 根据上架单Id查询明细
	@RequestMapping(value = "qryById")
	public ResponseEntity<ModelMap> queryPutOnDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Long id) {	
		PutOn data =putOnService.queryPutOnDetailList(id);
		List<PutOnDetail> detail = putOnService.qryPutOnDetailVo(id);
		modelMap.put("dataVo", detail);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 新增上架单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param PutOn
	 * @return
	 */
	@OperateLog(module = "上架管理",desc="添加上架单", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody PutOn putOn) {
		putOnService.create(putOn);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 编辑领用详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@OperateLog(module = "上架管理",desc="编辑上架单", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody PutOn putOn) {
		putOnService.edit(putOn);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 修改上架U位信息
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@OperateLog(module = "上架管理",desc="确认上架", type = OpType.UPDATE)
	@RequestMapping(value = "editShelfUp")
	public ResponseEntity<ModelMap> editShelfUp(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody PutOn putOn) {
		try {
			putOnService.editShelfUp(putOn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 删除领用详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "上架管理",desc="删除上架单", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		putOnService.remove(id);
		return setSuccessModelMap(modelMap, null);
	}
	/**
	 * 所在机房,设备类型,品牌,设备名称模糊查找资产
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "querAssetListByParam")
	public ResponseEntity<ModelMap> queryDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Map<String, Object> params) {	
		@SuppressWarnings("unchecked")
		Page<ItAssetVo> page = (Page<ItAssetVo>) super.getPage(params);
		Page<ItAssetVo> data = putOnService.querAssetListByParam(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	
}
